package com.CS3340.project;


//how to use this class
//judge j1 = new judge(playerhand1, playerhand2);
//int result = j1.result()   (if result is 1, player 1 wins. if result is 2, player 2 wins)
//if (result ==1){
//	player1.addmoney = pot1.payout()
//	pot1.setzero()}
//else
//player2.addmoney = pot1.payout()
//pot1.setzero()}

public class judge {
	private 
	card[] player1_hand;
	card[] player2_hand;
	int player1_score;
	int player2_score;
	public
	
	judge(){
		
	}
	
	judge(card[] in_cards1, card[] in_cards2){
		player1_hand = in_cards1;
		player2_hand = in_cards2;
		player1_score = handanalyzer(player1_hand);
		player2_score = handanalyzer(player2_hand);
	}
	
	int result(){
		int result =0;
		if (player1_score > player2_score)
			result =1;
		else 
			player2_score = 2;
		return result;
	}
	
	int handanalyzer(card[] inhand){
		int score = 0;
		card[] hand = new card[5];
		hand = inhand;
		sort(hand);
		
			if (hand[0].getNumber()==1){// check if there are ace
				
				//Straight flash A2345
				if ((hand[0].getNumber()==hand[1].getNumber()-1)&&(hand[0].getNumber()==hand[2].getNumber()-2)&&(hand[0].getNumber()==hand[3].getNumber()-3)&&(hand[0].getNumber()==hand[4].getNumber()-4)
						&&(hand[0].getSuit()==hand[1].getSuit())&&(hand[0].getSuit()==hand[2].getSuit())&&(hand[0].getSuit()==hand[3].getSuit())&&hand[0].getSuit()==hand[4].getSuit())
				{
					score = 8000;
					score += hand[4].getNumber()*10;
					score += hand[4].checksuit();
					}
				
				//Straight A2345
				else if	((hand[0].getNumber()==hand[1].getNumber()-1)&&(hand[0].getNumber()==hand[2].getNumber()-2)&&(hand[0].getNumber()==hand[3].getNumber()-3)&&(hand[0].getNumber()==hand[4].getNumber()-4))
				{
					score = 4000;
					score += hand[4].getNumber()*10;
					score += hand[4].checksuit();
				}
				
				for (int i=0;i<5;i++){
					if (hand[i].getNumber()==1)
						hand[i].changeace();
				}
				sort(hand);
			}
			
			//regular Straight flash
			if((hand[0].getNumber()==hand[1].getNumber()-1)&&(hand[0].getNumber()==hand[2].getNumber()-2)&&(hand[0].getNumber()==hand[3].getNumber()-3)&&(hand[0].getNumber()==hand[4].getNumber()-4)
					&&(hand[0].getSuit()==hand[1].getSuit())&&(hand[0].getSuit()==hand[2].getSuit())&&(hand[0].getSuit()==hand[3].getSuit())&&hand[0].getSuit()==hand[4].getSuit())
				{
				score = 8000;
				score += hand[4].getNumber()*10;
				score += hand[4].checksuit();
				}
			
			//Four of a kind
			else if (((hand[0].getNumber()==hand[1].getNumber()&&hand[0].getNumber()==hand[2].getNumber()&&hand[0].getNumber()==hand[3].getNumber()))
				||((hand[1].getNumber()==hand[2].getNumber()&&hand[1].getNumber()==hand[3].getNumber()&&hand[1].getNumber()==hand[4].getNumber())))
			{
				score = 7000;
				if (((hand[0].getNumber()==hand[1].getNumber()&&hand[0].getNumber()==hand[2].getNumber()&&hand[0].getNumber()==hand[3].getNumber())))
					score += hand[3].getNumber()*10;
				else
					score += hand[4].getNumber()*10;
			}
			
			//full house
			else if ((hand[0].getNumber()==hand[1].getNumber())&&(hand[0].getNumber()==hand[2].getNumber())&&(hand[3].getNumber()==hand[4].getNumber())
					||(hand[2].getNumber()==hand[3].getNumber())&&(hand[2].getNumber()==hand[4].getNumber())&&(hand[0].getNumber()==hand[1].getNumber()))
				{
				score = 6000;
				if ((hand[0].getNumber()==hand[1].getNumber())&&(hand[0].getNumber()==hand[2].getNumber())&&(hand[3].getNumber()==hand[4].getNumber()))
					score += hand[2].getNumber()*10;
				else
					score += hand[4].getNumber()*10;
					
				
				}
			
			//flush
			else if ((hand[0].getSuit()==hand[1].getSuit())&&(hand[0].getSuit()==hand[2].getSuit())&&(hand[0].getSuit()==hand[3].getSuit())&&(hand[0].getSuit()==hand[4].getSuit()))
			{
				score = 5000;
				score += hand[4].getNumber()*10;
				score += hand[4].checksuit();
			}
			
			
			//straight
			else if ((hand[0].getNumber()==hand[1].getNumber()-1)&&(hand[0].getNumber()==hand[2].getNumber()-2)&&(hand[0].getNumber()==hand[3].getNumber()-3)&&(hand[0].getNumber()==hand[4].getNumber()-4)){
				{
					score = 4000;
					score += hand[4].getNumber()*10;
					score += hand[4].checksuit();
				}
				
			}
			
			//three of a kind
			else if ((hand[0].getNumber()==hand[1].getNumber())&&(hand[0].getNumber()==hand[2].getNumber())
					||(hand[1].getNumber()==hand[2].getNumber())&&(hand[1].getNumber()==hand[3].getNumber())
					||(hand[2].getNumber()==hand[3].getNumber())&&(hand[2].getNumber()==hand[4].getNumber())){
				score = 3000;
				if ((hand[0].getNumber()==hand[1].getNumber())&&(hand[0].getNumber()==hand[2].getNumber()))
				score += hand[2].getNumber()*10;
				else if ((hand[1].getNumber()==hand[2].getNumber())&&(hand[1].getNumber()==hand[3].getNumber()))
					score += hand[3].getNumber()*10;
				else 
					score +=hand[4].getNumber()*10;
			}
			
			//two pairs
			else if ((hand[0].getNumber()==hand[1].getNumber())&&(hand[2].getNumber()==hand[3].getNumber())
					||(hand[0].getNumber()==hand[1].getNumber())&&(hand[3].getNumber()==hand[4].getNumber())
					||(hand[1].getNumber()==hand[2].getNumber())&&(hand[3].getNumber()==hand[4].getNumber())){
				score = 2000;
				if ((hand[0].getNumber()==hand[1].getNumber())&&(hand[2].getNumber()==hand[3].getNumber())){
					if (hand[2].checksuit()>hand[3].checksuit())
						score +=hand[2].checksuit();
					else
						score+=hand[3].checksuit();
				}
				else if ((hand[0].getNumber()==hand[1].getNumber())&&(hand[3].getNumber()==hand[4].getNumber())){
					if (hand[3].checksuit()>hand[4].checksuit())
						score +=hand[3].checksuit();
					else
						score+=hand[4].checksuit();
				}
				else
				{
					if (hand[3].checksuit()>hand[4].checksuit())
						score +=hand[3].checksuit();
					else
						score+=hand[4].checksuit();
				}
			}
			
			//one pair
			else if ((hand[0].getNumber()==hand[1].getNumber())
					||(hand[1].getNumber()==hand[2].getNumber())
					||(hand[2].getNumber()==hand[3].getNumber())
					||(hand[3].getNumber()==hand[4].getNumber()))
			{
				score = 1000;
				if (hand[0].getNumber()==hand[1].getNumber())
				{
					score +=hand[1].getNumber()*10;
					if (hand[0].checksuit()>hand[1].checksuit())
						score +=hand[0].checksuit();
					else
						score +=hand[1].checksuit();
				}
				else if (hand[1].getNumber()==hand[2].getNumber())
				{
					score +=hand[2].getNumber()*10;
					if (hand[1].checksuit()>hand[2].checksuit())
						score +=hand[1].checksuit();
					else
						score +=hand[2].checksuit();
				}
				else if (hand[2].getNumber()==hand[3].getNumber())
				{
					score +=hand[3].getNumber()*10;
					if (hand[2].checksuit()>hand[3].checksuit())
						score +=hand[2].checksuit();
					else
						score +=hand[3].checksuit();
				}
				else
				{
					score +=hand[4].getNumber()*10;
					if (hand[3].checksuit()>hand[4].checksuit())
						score +=hand[3].checksuit();
					else
						score +=hand[4].checksuit();
				}
			}
			
			//highest card
			else{
				score = hand[4].getNumber() * 10;
				if (hand[4].getSuit()=='s')
					score +=4;
				else if (hand[4].getSuit()=='h')
					score +=3;
				else if (hand[4].getSuit()=='c')
					score +=2;
				else 
					score +=1;
			}
		
			for (int i=0;i<5;i++){
				if (hand[i].getNumber()==14)
					hand[i].changeace2();
			}
		
		return score;
	}
	
	card[] sort(card[] incard){
		card[] hand = new card[5];
		int n = hand.length;
		
		if (incard.length !=5){
			System.out.println("error");
		}
		else{
		hand = incard;
		card temp= new card();
		for (int i=0;i<n;i++){
			for (int j=1;j<(n-i);j++)
				if (hand[j-1].getNumber()>hand[j].getNumber()){
					temp=hand[j-1];
					hand[j-1]=hand[j];
					hand[j]=temp;
				}
		}
			
		
		}
		return hand;
	}	
}